import { createWebHistory, createRouter } from "vue-router";

const Layout = () => import(/** layout */ "@/layout");

const ErrorPage = () => import(/** error */ "@/views/error/404.vue");

const ContractShare = () => import(/** contract */ "@/views/contract/contractShare");
const ContractDetail = () => import(/** contract */ "@/views/contract/contractDetail");

// 公共路由
export const constantRoutes = [
  {
    path: "/:pathMatch(.*)*",
    component: ErrorPage,
    hidden: true
  },
  {
    path: "/",
    component: Layout,
    redirect: "/contract-share",
    children: [
      {
        path: "/contract-share",
        component: ContractShare,
        name: "contractShare",
        meta: { title: "输入密码" }
      },
      {
        path: "/contract-detail",
        component: ContractDetail,
        name: "contractDetail",
        meta: { title: "查看合同" }
      }
    ]
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  }
});

router.beforeEach(async (to, from, next) => {
  next();
});

export default router;
